<?php

namespace app\api\controller\classes;

use app\common\controller\Api;
use app\common\model\Device;
use app\common\model\DeviceAssociation;
use app\common\model\Student;

/**
 * 班牌
 */
class Card extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (班牌人脸库)
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/classes/card/faces)
     * @ApiParams   (name="device_code", type="string", required=true, description="激活码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1569292928",
    "data": [
    {
    "id": 1,
    "number": "9310",
    "name": "金满堂",
    "gender": 0,
    "grade_id": 3,
    "class_id": 4,
    "avatar": "https://qn.keji110.com/%E9%87%91%E6%BB%A1%E5%A0%82.jpg"
    },
    {
    "id": 2,
    "number": "18414249",
    "name": "zhenzihan",
    "gender": 0,
    "grade_id": 3,
    "class_id": 4,
    "avatar": "http://qn.keji110.com/fasedatabase_20190725095442/ek0m9HX4cMnm5WjVXVZ3owgAwyJ5VqYACYAsDngm.jpeg"
    }
    ]
    })
     */
    public function faces()
    {
        $device_code = $this->request->request('device_code');
        if (!$device_code) {
            $this->error('参数错误');
        }

        $device_key = Device::where('device_code', $device_code)->where('status', Device::ACTIVATED)->find();
        if (!$device_key) {
            $this->error('激活码有误');
        }

        $device_association = DeviceAssociation::where('device_id', $device_key['id'])->find();
        if (!$device_association) {
            $this->error('该班牌暂未绑定班级');
        }

        $students = Student::where('class_id', $device_association['class_id'])
            ->where('status', Student::DISPLAY)
            ->field('id,number,name,gender,grade_id,class_id,avatar')
            ->select();

        foreach ($students as $key => $student) {

            if ($student['avatar']) {
                if (!strstr($student['avatar'], 'http') && !strpos($student['avatar'], 'https')) {
                    if (strpos($student['avatar'], '/uploads') > -1) {
                        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://':"http://";
                        $students[$key]['avatar'] = $http_type . $_SERVER['HTTP_HOST'] . $student['avatar'];
                    } else {
                        $students[$key]['avatar'] = 'http://qn.keji110.com/' . $student['avatar'];
                    }
                }
            }
        }

        $this->success('', $students);
    }

}
